<?php
class DisplayHelper extends AppHelper{
    var $helpers = array('Html');
    
    public function link($data,$type =null){
       echo $this->getLink($data, $type);
    }
    
    public function getLink($data,$type =null){
        $l = '';
         if ($type != null){
            switch ($type){
                case 'PlaceCity':
                    $name = $this->getText($data, $type);
                    $id = $data['places_id'];
                   $l = $this->Html->link($name, array('controller' => 'PlaceCities', 'action' => 'view', $id));
                break;
                case 'PlaceDepartment':
                    $name = $this->getText($data, $type);
                    $id = $data['places_id'];
                    $l = $this->Html->link($name, array('controller' => 'PlaceDepartments', 'action' => 'view', $id));
                break;
                case 'PlaceState':
                    $name = $this->getText($data, $type);
                    $id = $data['places_id'];
                    $l = $this->Html->link($name, array('controller' => 'PlaceStates', 'action' => 'view', $id));
                break;
                case 'PlaceCountry':
                    $name = $this->getText($data, $type);
                    $id = $data['places_id'];
                    $l = $this->Html->link($name, array('controller' => 'PlaceCountries', 'action' => 'view', $id));
                break;
                case 'User':
                    $name = $this->getText($data, $type);
                    $id = $data['id'];
                    $l = '<span class="user">'.$this->Html->link($name, array('controller' => 'Users', 'action' => 'view', $id)).'</span>';
                break;
                case 'ActionPoll':
                    $name = $this->getText($data, $type);
                    $id = $data['actions_id'];
                    $l = '<span class="actionPoll">'.$this->Html->link($name, array('controller' => 'ActionPolls', 'action' => 'view', $id)).'</span>';
                break;
                case 'ActionPost':
                    $name = $data['title'];
                    $l = '<span class="actionPost">'.$name.'</span>';
                break;
                case 'ActionEvent':
                    $name = $data['title'];
                    $id = $data['id'];
                    $l = '<span class="actionEvent">'.$this->Html->link($name, array('controller' => 'ActionEvents', 'action' => 'view', $id)).'</span>';
                break;
                case 'ActionLaw':
                    $name = $data['title'];
                    $id = $data['id'];
                    $l = '<span class="actionEvent">'.$this->Html->link($name, array('controller' => 'ActionLaws', 'action' => 'view')).'</span>';
                break;
                case 'Group':
                    $name = $this->getText($data,$type);
                    $id = $data['id'];
                    $l = $this->Html->link($name, array('controller' => 'Groups', 'action' => 'view', $id));
                break;
            }
        }
        else{
            $l = "Pas encore codé ! ";
        }
        
        return $l;
    }
    
    public function getText($data,$type =null){
         if ($type != null){
            switch ($type){
                case 'PlaceCity':
                    $name = $data['name'].' ('.$data['zip_code'].')';
                break;
                case 'PlaceDepartment':
                    $name = $data['name'].' ('.$data['num'].')';
                break;
                case 'PlaceState':
                    $name = $data['name'];
                break;
                case 'PlaceCountry':
                    $name = $data['name'].' ('.$data['code'].')';
                break;
                case 'User':
                    $name = $data['surname'].' '.$data['name'];
                break;
                case 'Group':
                    $name = $data['name'];
                break;
                case 'ActionPoll':
                    $name = $data['title'];
                break;
                case 'Group.status':
                    if ($data == 'active'){
                        $name = 'Actif';
                    }
                    else{
                        $name = 'En proposition <span class="help"> Un groupe en proposition est un groupe avec peu d\'action disponible. 
                                Il sera un groupe au status actif si assez de personne décide de la rejoindre. </span>';                        
                    }
                break;
                default:
                    $name = "A coder ! ";
            }
        }
        else{
            $name = "Pas encore codé ! ";
        }
        
        return $name;
    }
    public function text($data,$type =null){
        echo $this->getText($data,$type);
    }
    
    
    public function avatar($avatar, $user = null){
        if ($user != null){
            $url = array(
                'action' => 'view', 
                'controller' => 'Users',
                $user['id']
            );
        }
        else{
            if (!empty($avatar['Image']) && !empty($avatar['User'])){
                $user = $avatar['User'];
                $avatar = $avatar['Image'];
                $url = array(
                    
                    'action' => 'view', 
                    'controller' => 'Users',
                    $user['id']
                );
            }
            else
                $url = array();
        }
        
        if (!empty($avatar['filename'])){
            echo $this->Html->image('../'.$avatar['filename'], array(
                'height' => 100,
                'width' => 100,
                'url' => $url));

            echo $this->getLink($user, 'User');
        }
        else{
            echo 'Aucun Avatar';
        }
    }
    
    public function getStatus($userGroup){
        switch ($userGroup['status']){
            case 'in':
                $val = "Membre du Groupe";
            break;
            case 'ask':
                $val = "Demande en traitement";
            break;
            default:
                $val = "A coder ";
        }
        
        return $val;
    }
    
    
    public function percent($nb){
        return '<div class="graph" style="border : 1px solid black;"> <div style="width:'.$nb.'%; background:green; padding:2px 10px;"> '. round($nb, 1).'/100' .'</div></div>';
    }
}
?>
